package com.eedi.org.hospital.service;

import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.org.hospital.controller.vo.dept.OrgHospDeptPageReqVO;
import com.eedi.org.hospital.controller.vo.dept.OrgHospDeptSaveReqVO;
import com.eedi.org.hospital.dal.dataobject.OrgHospDeptDO;
import com.eedi.org.hospital.dal.mapper.OrgHospDeptMapper;
import com.eedi.org.hospital.facade.OrgDeptFacade;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.org.enums.OrgErrorCodeConstants.HOSPITAL_DEPT_DOCTOR_EXIST_AVOID_DEPT;
import static com.eedi.org.enums.OrgErrorCodeConstants.HOSPITAL_DEPT_SAME_NAME_EXIST;
import static com.eedi.org.enums.OrgErrorCodeConstants.HOSP_DEPT_NOT_EXISTS;


/**
 * 医院科室 Service 实现类
 *
 * @author 永聚长青源码
 */
@Service
@Validated
public class OrgHospDeptServiceImpl implements OrgHospDeptService {

    @Resource
    private OrgHospDeptMapper orgHospDeptMapper;

    @Resource
    private OrgDeptFacade orgDeptFacade;

    @Override
    public String createHospDept(OrgHospDeptSaveReqVO createReqVO) {
        //根据名称判重，不允许同名插入
        if (orgHospDeptMapper.selectByName(createReqVO.getOrgDeptName(),createReqVO.getOrgHospitalId()) != null) {
            throw exception(HOSPITAL_DEPT_SAME_NAME_EXIST);
        }
        // 插入
        OrgHospDeptDO hospDept = BeanUtils.toBean(createReqVO, OrgHospDeptDO.class);
        orgHospDeptMapper.insert(hospDept);
        // 返回
        return hospDept.getOrgHospDeptId();
    }

    @Override
    public void updateHospDept(OrgHospDeptSaveReqVO updateReqVO) {
        // 校验存在
        validateHospDeptExists(updateReqVO.getOrgHospDeptId());

        //根据名称判重，不允许同名插入
        OrgHospDeptDO existHospDept = orgHospDeptMapper.selectByName(updateReqVO.getOrgDeptName(), updateReqVO.getOrgHospitalId());
        if (existHospDept != null && !existHospDept.getOrgHospDeptId().equals(updateReqVO.getOrgHospDeptId())) {
            throw exception(HOSPITAL_DEPT_SAME_NAME_EXIST);
        }
        // 更新
        OrgHospDeptDO updateObj = BeanUtils.toBean(updateReqVO, OrgHospDeptDO.class);
        orgHospDeptMapper.updateById(updateObj);
    }

    @Override
    public void deleteHospDept(String orgHospDeptId) {
        // 校验存在
        OrgHospDeptDO orgHospDeptDO = validateHospDeptExists(orgHospDeptId);

        //校验医院下是否有医生存在
        if (orgDeptFacade.validExistDept(orgHospDeptDO.getOrgHospitalId(), orgHospDeptId)) {
            throw exception(HOSPITAL_DEPT_DOCTOR_EXIST_AVOID_DEPT);
        }
        // 删除
        orgHospDeptMapper.deleteById(orgHospDeptId);
    }

    private OrgHospDeptDO validateHospDeptExists(String orgHospDeptId) {
        OrgHospDeptDO orgHospDeptDO = orgHospDeptMapper.selectById(orgHospDeptId);
        if (orgHospDeptMapper.selectById(orgHospDeptId) == null) {
            throw exception(HOSP_DEPT_NOT_EXISTS);
        }
        return orgHospDeptDO;
    }

    @Override
    public OrgHospDeptDO getHospDept(String orgHospDeptId) {
        return orgHospDeptMapper.selectById(orgHospDeptId);
    }

    @Override
    public void updateHospDeptStatus(String orgHospDeptId, CommonStatusEnum status) {
        // 校验存在
        validateHospDeptExists(orgHospDeptId);

        // 更新状态
        OrgHospDeptDO updateObj = new OrgHospDeptDO();
        updateObj.setOrgHospDeptId(orgHospDeptId);
        updateObj.setOrgStatus(status);
        orgHospDeptMapper.updateById(updateObj);
    }

    @Override
    public PageResult<OrgHospDeptDO> getHospDeptPage(OrgHospDeptPageReqVO pageReqVO) {
        return orgHospDeptMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OrgHospDeptDO> listByIds(List<String> orgHospDeptIds) {
        if (CollectionUtils.isEmpty(orgHospDeptIds)) {
            return Collections.emptyList();
        }
        return orgHospDeptMapper.selectBatchIds(orgHospDeptIds);
    }

}
